package com.zgjkhis.modules.charges.rest;

import com.zgjkhis.domain.PageResult;
import com.zgjkhis.exception.ServiceException;
import com.zgjkhis.logging.aop.log.Log;
import com.zgjkhis.modules.charges.domain.Invoice;
import com.zgjkhis.modules.charges.service.InvoiceService;
import com.zgjkhis.modules.charges.service.dto.InvoiceQueryCriteria;
import com.zgjkhis.modules.security.security.LoginUserUtil;
import com.zgjkhis.utils.SecurityUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.data.domain.Pageable;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * @Author yzq
 * @Description 发票管理
 * @Date 2021/7/13 18:10
 * @Param
 * @return
 **/
@Api(tags = "收费：发票管理")
@RestController
@RequestMapping("/api/invoice")
public class InvoiceController {


    private static final String ENTITY_NAME = "invoice";
    private final InvoiceService invoiceService;

    public InvoiceController(InvoiceService invoiceService) {
        this.invoiceService = invoiceService;
    }


    @Log("新增发票")
    @ApiOperation("新增发票")
    @PostMapping(value = "/add")
//    @PreAuthorize("@el.check('invoice:add')")
    public ResponseEntity<Object> create(@Validated @RequestBody Invoice resources) {
        if (resources.getId() != null) {
            throw new ServiceException("A new " + ENTITY_NAME + " cannot already have an ID");
        }
        resources.setCreateUserId(SecurityUtils.getUserId().toString());
        resources.setCreateUserName(LoginUserUtil.getNickName());
        resources.setPharmacyId(LoginUserUtil.getPharmId());
        return new ResponseEntity<>(invoiceService.save(resources), HttpStatus.OK);
    }

    @Log("修改发票")
    @ApiOperation("修改发票")
    @PostMapping(value = "/update")
//    @PreAuthorize("@el.check('supplier:edit')")
    public ResponseEntity<Object> update(@Validated @RequestBody Invoice resources) {
        invoiceService.saveOrUpdate(resources);
        return ResponseEntity.ok(true);
    }

    @Log("修改发票打印次数")
    @ApiOperation("修改发票打印次数")
    @PostMapping(value = "/updatePrintTimes")
//    @PreAuthorize("@el.check('supplier:edit')")
    public ResponseEntity<Object> updatePrintTimes(String id) {
        Invoice invoice = invoiceService.getById(id);
        Invoice temp = new Invoice();
        temp.setId(invoice.getId());
        temp.setPrintingTimes(invoice.getPrintingTimes() + 1);
        invoiceService.updateById(temp);
        return ResponseEntity.ok(true);
    }


    @Log("分页查询发票")
    @ApiOperation("分页查询发票")
    @GetMapping(value = "/getList")
    public ResponseEntity<PageResult<Invoice>> getPrescriptions(InvoiceQueryCriteria criteria, Pageable pageable) {
        criteria.setPharmacyId(LoginUserUtil.getPharmId());
        return new ResponseEntity<>(invoiceService.findAllByPageable(criteria, pageable), HttpStatus.OK);
    }

}
